//
//  LoadingView.m
//  CeKe
//
//  Created by 李      涛 on 15/5/9.
//  Copyright (c) 2015年 李      涛. All rights reserved.
//

#import "LoadingView.h"

@interface LoadingView()

@property (nonatomic,strong) UIView * bgView;
@property (nonatomic,strong) UIActivityIndicatorView * activityView;
@property (nonatomic,strong) UILabel * contentLabel;

@end


@implementation LoadingView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)init{
    
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        self.hidden = YES;
        [self setupRootViewSubs:self];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.hidden = YES;
        [self setupRootViewSubs:self];
    }
    return self;
}



/**
 *  添加基础布局
 *
 *  @param viewParent 父布局
 */
- (void)setupRootViewSubs:(UIView *)viewParent{
    _bgView = [[UIView alloc] init];
    _bgView.layer.cornerRadius = 1;
    _bgView.layer.masksToBounds = YES;
    _bgView.backgroundColor = [UIColor blackColor];
    _bgView.alpha = 0.8;
    _bgView.center = self.center;
    
    CGFloat startX = (CGRectGetWidth(self.frame) - 120)/2;
    CGFloat startY = (CGRectGetHeight(self.frame)-90)/2  - 64;
    _bgView.frame = CGRectMake(startX, startY, 120, 90);
    [viewParent addSubview:_bgView];
    [self setupBgViewSubs:_bgView];
}


/**
 *  添加内容 和 菊花
 *
 *  @param viewParent 父布局
 */
- (void)setupBgViewSubs:(UIView *)viewParent{
    
    _activityView = [[UIActivityIndicatorView alloc] init];
    _activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    _activityView.color = [UIColor whiteColor];
    [viewParent addSubview:_activityView];
    _activityView.hidesWhenStopped = NO;
    
    
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.font = [UIFont systemFontOfSize:14];
    _contentLabel.text = @"正在加载";
    _contentLabel.textAlignment = NSTextAlignmentCenter;
    _contentLabel.numberOfLines = 0;
    _contentLabel.textColor = [UIColor whiteColor];
    _contentLabel.backgroundColor = [UIColor clearColor];
    [viewParent addSubview:_contentLabel];
    
    _hidesWhenStopped = YES;
    
    [_contentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_bgView.bottom).offset(-16);
        make.centerX.equalTo(_bgView.centerX);
    }];
    
    [_activityView makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_contentLabel.top).offset(-10);
        make.centerX.equalTo(_bgView.centerX);
    }];
    
}

/**
 *  开始动画
 */
- (void)startAnimating{
    [[self superview] bringSubviewToFront:self];
    [_activityView startAnimating];
    self.hidden = NO;
}

/**
 *  关闭动画
 */
- (void)stopAnimating{
    [_activityView stopAnimating];
    self.hidden = _hidesWhenStopped;
}

/**
 *  动画状态
 *
 *  @return 是否正在显示
 */
- (BOOL)isAnimating{
    return [_activityView isAnimating];
}


/**
 *  改变文字内容
 *
 *  @param contentText 内容
 */
- (void)setContentText:(NSString *)contentText{
    _contentText = contentText;
    _contentLabel.text = _contentText;
}

@end
